package com.book.upms.login.controller;

import com.book.common.api.core.RESTfulMethod;
import com.book.common.codec.Md5Utils;
import com.book.common.result.Failed;
import com.book.common.result.RestResponse;
import com.book.common.result.Success;
import com.book.modules.annotation.InjectionCurrentUser;
import com.book.modules.core.controller.BaseController;
import com.book.oauth.server.user.dto.UserDto;
import com.book.oauth.server.user.hystrix.IOauthHystrix;
import com.book.util.CaptchaUtil;
import com.wf.captcha.Captcha;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Description：
 *
 * @Author： leo.xiong
 * @CreateDate： 2019/10/14 10:26
 * @Email： leo.xiong@suyun360.com
 * Version：1.0
 */
@Slf4j
@RestController
@Api(value = "登录注册")
@RequestMapping(value = "${adminPath}")
public class LoginController extends BaseController {

    @Autowired
    private IOauthHystrix oauthHystrix;

    @PostMapping("/login")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "登录：用户名，密码不能为空", httpMethod = RESTfulMethod.POST, response = Success.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true),
            @ApiImplicitParam(name = "password", value = "密码", required = true)
    })
    public RestResponse login(@RequestParam(value = "username") String username, @RequestParam(value = "password") String password) {
        if (StringUtils.isEmpty(username)) {
            return new Failed("BOOK:PUBLIC:10000", i18nHelper.get("BOOK:PUBLIC:10000"));
        }
        if (StringUtils.isEmpty(password)) {
            return new Failed("BOOK:PUBLIC:10001", i18nHelper.get("BOOK:PUBLIC:10001"));
        }
        //获取token
        Map<String, String> tokenMap = currentUserHelper.getOauthToken(username, Md5Utils.md5(password), "password");
        if (tokenMap == null) {
            return new Failed("BOOK:CORE:10008", i18nHelper.get("BOOK:CORE:10008"));
        }
        return new Success(tokenMap, i18nHelper.get("BOOK:PUBLIC:10002"));
    }

    @PostMapping("images/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        CaptchaUtil.outPng(110, 34, 4, Captcha.TYPE_ONLY_NUMBER, request, response);
    }

    @PostMapping("registered")
    @ResponseBody
    @InjectionCurrentUser
    @ApiOperation(value = "注册：用户名，密码，手机号不能为空", httpMethod = RESTfulMethod.POST, response = Success.class)
    public RestResponse registered(UserDto userDto) {
        // 更新登录时间
        try {
            if (StringUtils.isEmpty(userDto.getUsername())) {
                return new Failed("BOOK:CORE:10005", i18nHelper.get("BOOK:CORE:10005"));
            }
            if (StringUtils.isEmpty(userDto.getPassword())) {
                return new Failed("BOOK:CORE:10006", i18nHelper.get("BOOK:CORE:10006"));
            }
            if (StringUtils.isEmpty(userDto.getMobile())) {
                return new Failed("BOOK:CORE:10007", i18nHelper.get("BOOK:CORE:10007"));
            }
            return oauthHystrix.registered(userDto);
        } catch (Exception e) {
            log.error("BOOK:COMMON:10033", e);
            return new Failed("BOOK:COMMON:10033", i18nHelper.get("BOOK:COMMON:10033"));
        }
    }

    @PostMapping("/logout")
    @ResponseBody
    @ApiOperation(value = "退出", httpMethod = RESTfulMethod.POST, response = Success.class)
    public RestResponse logout() {
        return currentUserHelper.logout();
    }
}
